/*
  * desc  :游戏中用到的数据 和存储数据
  * author:<zhouzhiwei>
  * @param:
  * date  : 2019/1/28
*/
function GameData() {
    this.isFaceBook = false;
    this.game_play=null;
    this.game_start=null,
    this.select_hero=null;
    this.hero=[
        {
            id:1,
            name:"运动员",                                                   /*英雄名字*/
            hp:500,
            attack:112,                                                     /*攻击力*/
            jianshang:"10",                                                 /*百分分数 10%*/
            content:3,                                                      /*子弹容量*/
            cd:1.5,                                                         /*冷却cd*/
            crit:0,                                                         /*暴击率 百分数*/
            price:200                                                    /*英雄价格*/
        },{
            id:2,
            name:"护士",                                                   /*英雄名字*/
            hp:550,
            attack:118,                                                     /*攻击力*/
            jianshang:"15",                                                 /*百分分数 10%*/
            content:4,                                                      /*子弹容量*/
            cd:2,                                                         /*冷却cd*/
            crit:10,                                                         /*暴击率 百分数*/
            price:600                                                    /*英雄价格*/
        },{
            id:3,
            name:"厨师",                                                   /*英雄名字*/
            hp:600,
            attack:134,                                                     /*攻击力*/
            jianshang:"25",                                                 /*百分分数 10%*/
            content:2,                                                      /*子弹容量*/
            cd:1.5,                                                         /*冷却cd*/
            crit:0,                                                         /*暴击率 百分数*/
            price:1000                                                    /*英雄价格*/
        },{
            id:4,
            name:"特种兵",                                                   /*英雄名字*/
            hp:700,
            attack:143,                                                     /*攻击力*/
            jianshang:"30",                                                 /*百分分数 10%*/
            content:4,                                                      /*子弹容量*/
            cd:2,                                                         /*冷却cd*/
            crit:15,                                                         /*暴击率 百分数*/
            price:1750                                                    /*英雄价格*/
        },{
            id:5,
            name:"宙斯",                                                   /*英雄名字*/
            hp:700,
            attack:143,                                                     /*攻击力*/
            jianshang:"20",                                                 /*百分分数 10%*/
            content:3,                                                      /*子弹容量*/
            cd:1.5,                                                         /*冷却cd*/
            crit:30,                                                         /*暴击率 百分数*/
            price:1750                                                    /*英雄价格*/
        },

    ]
    this.gameData = {
        coinNum:1000000,
        owned:[1],/*已拥有的英雄*/
        selectId:1,/*选择英雄id*/

    };
    this.getlocalDataOK = false;
    this.getlocalData = function (_key) {
        if (this.isFaceBook) {
            this.LoadFromFB();
        } else {
            var _value1 = cc.sys.localStorage.getItem(_key);
            this.getGameData(_value1);
        }
    };

    this.getGameData = function (_value1) {
        var _value;
        if (_value1) {
            _value = JSON.parse(_value1);
            // if (!_value.version || this.version != _value.version) {
            //     _value = this.fishData;
            // }
            this.gameData = _value;
        }
        this.getlocalDataOK = true;
    };
    /*
    *description: 通过键值对 保存数据
    *author: <zhouzhiwei>
    *@param:
    *date:
    */
    this.savelocalData = function (_value) {
        // console.log("savevalue   ", _value);
        var date = new Date();    //结束时间
        _value.saveTime = date.getTime();
        var _value1 = JSON.stringify(_value);
        if (this.isFaceBook) {
            // console.log("FBencrypted   ", encrypted);
            this.SaveToFB(encrypted);
        } else {
            cc.sys.localStorage.setItem("gameData", _value1);
        }
    };
    /**
     * FB存储
     */
    this.SaveToFB = function (obj) {
        if (this.isFaceBook) {
            FBInstant.player.setDataAsync({
                saveobj: obj,

            }).then(function () {
                console.log('FBdata is set');
            });
        }
    };
    this.LoadFromFB = function () {
        let self = this;
        if (this.isFaceBook) {
            FBInstant.player
                .getDataAsync(['saveobj'])
                .then(function (data) {
                    var saveobj = data['saveobj'];
                    console.log("FBsaveobj  ", saveobj);
                    self.getGameData(saveobj);
                });
        }
    };


    this.playMusic = function (str, bool) {
        if (this.bgMusic) {
            cc.audioEngine.playMusic(str, bool);
        }
    };
    this.stopMusic = function () {
        cc.audioEngine.pauseMusic();
    };
    this.playEffect = function (str) {
        if (this.bgEffect) {
            cc.audioEngine.playEffect(str, false, 1);
        }
    };
    this.stopAllEffects = function (str) {
        cc.audioEngine.stopAllEffects();
    };
    this.pauseMusic = function (str) {
        cc.audioEngine.pauseMusic();
    };
    this.resumeMusic = function (str) {
        cc.audioEngine.resumeMusic();
    };
    /*暂停播放所有音效*/
    this.pauseAllEffects = function () {

    }
    /*回复播放所有音效*/
    this.resumeAll = function () {

    }
    /*停止播放所有音效*/
    this.stopAllEffects = function () {

    }
}

var _instance = null;
module.exports = function () {
    if (!_instance) {
        _instance = new GameData();
    }
    return _instance;
};